import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/part_entity.dart';
import 'package:xin_nuo_pda/routes/Routes.dart';
import 'package:xin_nuo_pda/utils/MyDateUtils.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/ClearBorderEditText.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';

import 'EquipmentRepairDetailsController.dart';

class EquipmentRepairDetailsPage extends StatelessWidget {
  final controller = Get.put(EquipmentRepairDetailsController());
  final state = Get.find<EquipmentRepairDetailsController>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("设备维修详情"),
        ),
        body: SingleChildScrollView(
          child: Container(
            child: Column(children: [
              GetBuilder<EquipmentRepairDetailsController>(
                  builder: (controller) => Column(
                        children: [
                          TextView("设备ID:",
                              state.repairInfoEntity?.EQUIPMENTID ?? ''),
                          SizedBox(
                            height: 1,
                          ),
                          TextView("批次ID:", state.repairInfoEntity?.RESERVED1 ?? ''),
                          SizedBox(
                            height: 1,
                          ),
                          TextView(
                              "报修人:",
                              state.repairInfoEntity?.TROUBLEREPORTERNAME ??
                                  ''),
                          SizedBox(
                            height: 1,
                          ),
                          TextView("故障描述:",
                              state.repairInfoEntity?.TROUBLECOMMENT ?? ''),
                          SizedBox(
                            height: 1,
                          ),
                          TextView("报修时间:",
                              state.repairInfoEntity?.TROUBLETIME ?? ''),
                          SizedBox(
                            height: 1,
                          ),
                          TextView("报修备注:",
                              state.repairInfoEntity?.TROUBLEREASON ?? ''),
                          SizedBox(
                            height: 1,
                          ),
                          Container(
                            color: Colors.white,
                            padding: EdgeInsets.all(8),
                            alignment: Alignment.centerLeft,
                            child: Text(
                              "维修信息",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 16),
                            ),
                          ),
                          SizedBox(
                            height: 1,
                          ),
                          TitleEditText(
                            '操作员',
                            '请输入操作员',
                            (value) {},
                            isScan: false,
                            controller: state.userController,
                          ),
                          SizedBox(
                            height: 1,
                          ),
                          GetBuilder<EquipmentRepairDetailsController>(
                            builder: (controller) => Container(
                                padding: EdgeInsets.fromLTRB(12, 8, 12, 8),
                                color: Colors.white,
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: Text(
                                        "维修完成时间*:",
                                        style: TextStyle(
                                            color: theme_blue.toColor()),
                                      ),
                                      flex: 3,
                                    ),
                                    Expanded(
                                        flex: 7,
                                        child: Material(
                                          child: Ink(
                                            child: InkWell(
                                              onTap: () {
                                                MyDateUtils.showDateTime(context,
                                                    (date) {
                                                  controller
                                                      .updateSelectDate(date);
                                                },
                                                    currentTime:
                                                        state.selectDate);
                                              },
                                              child: Container(
                                                alignment: Alignment.centerLeft,
                                                padding:
                                                    EdgeInsets.only(left: 8),
                                                height: 36,
                                                decoration: BoxDecoration(
                                                    border: new Border.all(
                                                        width: 1.0,
                                                        color: Colors.grey),
                                                    borderRadius:
                                                        BorderRadius.all(
                                                            Radius.circular(
                                                                8))),
                                                child: Row(
                                                  children: [
                                                    Expanded(
                                                        child: Text(
                                                          state.selectDate.year
                                                                  .toString() +
                                                              "年" +
                                                              state.selectDate
                                                                  .month
                                                                  .toString() +
                                                              "月" +
                                                              state.selectDate
                                                                  .day
                                                                  .toString() +
                                                              "日" +
                                                              state.selectDate.hour
                                                                  .toString() +
                                                              '时' +
                                                              state.selectDate.minute
                                                                  .toString() +
                                                              '分',
                                                        ),
                                                        flex: 1),
                                                    Icon(
                                                      Icons.arrow_forward_ios,
                                                      size: 16,
                                                    ),
                                                    SizedBox(
                                                      width: 4,
                                                    )
                                                  ],
                                                ),
                                              ),
                                            ),
                                          ),
                                        ))
                                  ],
                                )),
                            id: state.dateChooseId,
                          ),
                          SizedBox(
                            height: 1,
                          ),
                          Container(
                              padding: EdgeInsets.fromLTRB(12, 8, 12, 8),
                              color: Colors.white,
                              child: Row(
                                children: [
                                  Expanded(
                                    child: Text(
                                      "维修说明:",
                                      style: TextStyle(
                                          color: theme_blue.toColor()),
                                    ),
                                    flex: 3,
                                  ),
                                  Expanded(
                                      flex: 7,
                                      child: ClearBorderEditText(
                                        "请填写维修说明",
                                        (value) {},
                                        controller: state.contentController,
                                      ))
                                ],
                              )),
                          SizedBox(
                            height: 1,
                          ),
                          Container(
                            color: Colors.white,
                            padding: EdgeInsets.all(8),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              children: [
                                Expanded(
                                    flex: 1,
                                    child: Text(
                                      "载具列表",
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 16),
                                    )),
                                Container(
                                  height: 32,
                                  margin: EdgeInsets.only(right: 8),
                                  child: ElevatedButton(
                                    onPressed: () async {
                                      var result = await Get.toNamed(
                                          Routes.AddParts,
                                          arguments: state.repairInfoEntity?.EQUIPMENTID ?? '');
                                      controller.addPartsData(
                                          result["partsName"],
                                          result["partsId"],
                                          );
                                    },
                                    child: Text("添加"),
                                  ),
                                )
                              ],
                            ),
                          ),
                          GetBuilder<EquipmentRepairDetailsController>(
                              builder: (controller) => Table(
                                    defaultVerticalAlignment:
                                        TableCellVerticalAlignment.middle,
                                    columnWidths: {
                                      //列宽
                                      0: FlexColumnWidth(1.0),
                                      1: FlexColumnWidth(2.0),
                                      2: FlexColumnWidth(2.0),
                                      3: FlexColumnWidth(1.0),
                                    },
                                    border: TableBorder.all(
                                      color: Colors.grey,
                                      width: 1.0,
                                      style: BorderStyle.solid,
                                    ),
                                    children: getPartsList(context),
                                  ),
                              id: state.partsId),
                          SizedBox(
                            height: 8,
                          ),
                          ButtonBar(
                            alignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              Container(
                                height: 32,
                                child: ElevatedButton(
                                  onPressed: () {
                                    controller.repairHandler('REPAIR-IDLE');
                                  },
                                  child: Text("完成转生产"),
                                ),
                              ),
                              Container(
                                height: 32,
                                child: ElevatedButton(
                                  onPressed: () {
                                    controller.repairHandler('REPAIR-PE');
                                  },
                                  child: Text("转PE处理"),
                                ),
                              ),
                              Container(
                                height: 32,
                                child: ElevatedButton(
                                  onPressed: () {
                                    controller.repairHandler('REAPIR-QA');
                                  },
                                  child: Text("转QA处理"),
                                ),
                              )
                            ],
                          ),
                          SizedBox(
                            height: 8,
                          ),
                          // Container(
                          //   color: Colors.white,
                          //   padding: EdgeInsets.all(8),
                          //   alignment: Alignment.centerLeft,
                          //   child: Text(
                          //     "维修信息",
                          //     style: TextStyle(
                          //         fontWeight: FontWeight.bold, fontSize: 16),
                          //   ),
                          // ),
                          // SizedBox(
                          //   height: 1,
                          // ),
                          // Table(
                          //   defaultVerticalAlignment:
                          //       TableCellVerticalAlignment.middle,
                          //   columnWidths: {
                          //     //列宽
                          //     0: FlexColumnWidth(1.0),
                          //     1: FlexColumnWidth(2.0),
                          //     2: FlexColumnWidth(1.0),
                          //     3: FlexColumnWidth(2.0),
                          //   },
                          //   border: TableBorder.all(
                          //     color: Colors.grey,
                          //     width: 1.0,
                          //     style: BorderStyle.solid,
                          //   ),
                          //   children: getTableRow(context),
                          // ),
                          SizedBox(
                            height: 12,
                          ),
                        ],
                      )),
            ]),
          ),
        ));
  }

  List<TableRow> getPartsList(BuildContext context) {
    List<TableRow> tempView = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '序号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '部件名称',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '编号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '操作',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.partsList.length; i++) {
      PartEntity bean = state.partsList[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              "${i + 1}",
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            bean.pARTSNAME ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            bean.pARTSID ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: IconButton(
            icon: Icon(
              Icons.delete,
              color: Colors.red,
            ),
            onPressed: () {
              controller.deletePartsData(bean);
            },
          ),
        ),
      ]));
    }
    return tempView;
  }
}
